package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.PostLoad;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * Created by sunears on 2018/12/28.
 */
@RestController
public class UsersController {
    @Autowired
    private UserRepo userRepo;
    @GetMapping("/hello")
    public String hello(){
        return "it's ok";
    }
    @PostMapping("/users")
    public User createUser(@RequestBody User user){
        return userRepo.save(user);
    }
    @GetMapping("/users")
    public List<User> getUsers(){
        return userRepo.findAll();
    }
    @GetMapping("/users/{id}")
    public User getUser(@PathVariable long id){
        return userRepo.findById(id);

    }

    @GetMapping("/users-by-name/{name}")
    public User getUser(@PathVariable String name){
        return userRepo.findByName(name);

    }
}
